//
//  shipSourceViewController.m
//  WHB_cargo
//
//  Created by 徐国庆 on 15/12/15.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "shipSourceViewController.h"
#import "shipSourceCell.h"
#import "CWStarRateView.h"
#import "GoodsList_ViewController.h"
#import "RouteLine_ViewController.h"

@interface shipSourceViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;

@end

@implementation shipSourceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navtitle.text = @"货源";
    [self initWithView];
}

#pragma mark UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * indentifer = @"Empty";
    
    shipSourceCell *cell = (shipSourceCell *)[tableView dequeueReusableCellWithIdentifier:indentifer];
    if (!cell)
    {
        cell=[[NSBundle mainBundle] loadNibNamed:@"Empty" owner:self options:nil][0];
        UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 10)];
        topLabel.backgroundColor = [UIColor HexStrWithColor:@"F0F0F0"];
        topLabel.layer.borderWidth = 0.5;
        topLabel.layer.borderColor = [UIColor HexStrWithColor:@"d0d0d0"].CGColor;
        [cell.contentView addSubview:topLabel];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.vechileImageView.frame = CGRectMake((kUIScreenWidth - 200) / 2, 20, 200, 120);
    cell.vechileImageView.backgroundColor = [UIColor redColor];
    [cell.vechileNumber addSubview:[self titleAndContent:@"车牌号:" andContent:@"京SU23EA" withToAddress:nil]];
    [cell.commonCarrmer addSubview:[self titleAndContent:@"承运商:" andContent:@"承运商全称" withToAddress:nil]];
    [cell.lineRoad addSubview:[self titleAndContent:@"线路:" andContent:@"深圳" withToAddress:@"上海"]];
    [cell.address addSubview:[self titleAndContent:@"位置:" andContent:@"哈尔滨市南岗区西大直街" withToAddress:nil]];
    [cell.distance addSubview:[self titleAndContent:@"距离:" andContent:@"1公里" withToAddress:nil]];
    [cell.shipTypeView addSubview:[self titleAndContent:@"品类:" andContent:@"汽油" withToAddress:nil]];
    [cell.weightView addSubview:[self titleAndContent:@"重量:" andContent:@"3吨" withToAddress:nil]];
    cell.weightBottomLineLabel.frame = CGRectMake(0, cell.weightView.frame.origin.y + cell.weightView.frame.size.height + 10, kUIScreenWidth, 1);
    cell.weightBottomLineLabel.backgroundColor = [UIColor redColor];
    // 推送货源按钮
    cell.pushShipSourceButton.backgroundColor = [UIColor HexStrWithColor:@"750000"];
    [cell.pushShipSourceButton addTarget:self action:@selector(pushShipButtonAction:) forControlEvents:UIControlEventTouchDown];
    // 拨打电话按钮
    cell.callingButton.backgroundColor = [UIColor HexStrWithColor:@"00BB00"];
    [cell.callingButton addTarget:self action:@selector(callingButtonAction:) forControlEvents:UIControlEventTouchDown];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 430;
}

#pragma mark 初始地 目的地 货品类型按钮点击方法
- (void)topButtonAction:(UIButton *)button{
    if (button.tag == 500) {
        NSLog(@"初始地");
        RouteLine_ViewController *lineVC = [[RouteLine_ViewController alloc]init];
        [self.navigationController pushViewController:lineVC animated:YES];
    } else if (button.tag == 501){
        NSLog(@"目的地");
        RouteLine_ViewController *lineVC = [[RouteLine_ViewController alloc]init];
        [self.navigationController pushViewController:lineVC animated:YES];
    } else if (button.tag == 502){
        NSLog(@"货品类型");
        // 使用block传值
        // 参数1：选中的物品种类
        void(^block)(NSString *name) = ^(NSString *name){
            
            [button setTitle:name forState:UIControlStateNormal];
            [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        };
        GoodsList_ViewController *goodsList = [[GoodsList_ViewController alloc]init];
        goodsList.sendProvince = block;
        [self.navigationController pushViewController:goodsList animated:YES];
    }
}

#pragma mark 推送货源点击事件
- (void)pushShipButtonAction:(UIButton *)button{
    NSLog(@"推送货源");
}

#pragma mark 拨打电话点击事件
- (void)callingButtonAction:(UIButton *)button{
    NSLog(@"拨打电话");
}

- (void)rightButtonAction:(UIButton *)button{
    NSLog(@"点击右侧按钮");
}

/**
 *  初始化列表
 */
- (void)initWithView{
    
    
    NSArray *tempArr = [[NSArray alloc] initWithObjects:@"初始地", @"目的地", @"货品类型", nil];
    // 创建初始地 目的地 货品类型按钮
    for (int i = 0; i < 3; i ++) {
        [self createButtons:[tempArr objectAtIndex:i] point_x:i * kUIScreenWidth / 3 buttonTag:i + 500];
    }
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, kUIScreenWidth, kUIScreenHeight - 20) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    [self.rightButton setImage:[UIImage imageNamed:@"sy"] forState:UIControlStateNormal];
    [self.rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchDown];
}

/**
 *  创建页面上方初始地 目的地 货品类型按钮
 *
 *  @param title     按钮标题
 *  @param point_x   按钮横向坐标
 *  @param buttonTag 按钮tag值
 */
- (void)createButtons:(NSString *)title point_x:(float)point_x buttonTag:(int)buttonTag{
    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
    tempButton.frame = CGRectMake(point_x, 60, kUIScreenWidth / 3, 40);
    tempButton.tag = buttonTag;
    tempButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [tempButton setTitle:title forState:UIControlStateNormal];
    [tempButton setTitleColor:[UIColor HexStrWithColor:@"3C3C3C"] forState:UIControlStateNormal];
    [tempButton addTarget:self action:@selector(topButtonAction:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:tempButton];
    
    // 按钮后面的箭头
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake( kUIScreenWidth / 3 - 20, 15, 15, 10)];
    imageView.image = [UIImage imageNamed:@"xia"];
    [tempButton addSubview:imageView];
}

/**
 *  自定义cell
 *
 *  @param title     标题
 *  @param content   内容
 *  @param toAddress 如果是地址显示到达的位置 如果该参数为nil自动设置为不显示
 *
 *  @return 生成一个标题和一个内容的view
 */
- (UIView *)titleAndContent:(NSString *)title andContent:(NSString *)content withToAddress:(NSString *)toAddress{
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:titleLabel];
    
    // 内容
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, 10, 200, 20)];
    contentLabel.text = content;
    contentLabel.textAlignment = 0;
    [contentLabel sizeToFit];
    contentLabel.font = [UIFont systemFontOfSize:15];
    // 如果标题为货单状态 改变详情颜色
    if ([title isEqualToString:@"货单状态:"]) {
        contentLabel.textColor = [UIColor HexStrWithColor:@"FF0000"];
    }
    
    [bottomView addSubview:contentLabel];
    
    if ([title isEqualToString:@"线路:"]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x + contentLabel.frame.size.width, contentLabel.frame.origin.y + 5, 18 / 2.0, 21 / 2.0)];
        imageView.image = [UIImage imageNamed:@"jt_xl"];
        //        imageView.transform=CGAffineTransformMakeRotation(1.55);
        [bottomView addSubview:imageView];
        
        UILabel *toAddLabel = [[UILabel alloc] initWithFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width, contentLabel.frame.origin.y , 200, 20)];
        toAddLabel.text = toAddress;
        [bottomView addSubview:toAddLabel];
    }
    
    return bottomView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
